Imports System.Data.SqlClient

Public Class cls_marcamodelo_

    Implements ICloneable

#Region "Variables ..."
    Private int_idmarcamodelo As Integer = 0
    Private int_idmarca As Integer = 0
    Private int_idmodelo As Integer = 0

    Private obj_marca As cls_marca_ = Nothing
    Private obj_modelo As cls_modelo_ = Nothing

    Private col_Items As Collections.Generic.List(Of cls_marcamodelo_) = Nothing
#End Region

#Region "Propiedades ..."
    Property idmarcamodelo() As Integer
        Get
            idmarcamodelo = int_idmarcamodelo
        End Get
        Set(ByVal value As Integer)
            int_idmarcamodelo = value
        End Set
    End Property
    Property idmarca() As Integer
        Get
            idmarca = int_idmarca
        End Get
        Set(ByVal value As Integer)
            int_idmarca = value
        End Set
    End Property
    Property idmodelo() As Integer
        Get
            idmodelo = int_idmodelo
        End Get
        Set(ByVal value As Integer)
            int_idmodelo = value
        End Set
    End Property
    Property marca() As cls_marca_
        Get
            If IsNothing(obj_marca) Then
                obj_marca = New cls_marca_
                If Not obj_marca.Buscar(idmarca) Then
                    obj_marca = Nothing
                End If
            End If
            marca = obj_marca
        End Get
        Set(ByVal value As cls_marca_)
            obj_marca = value
        End Set
    End Property
    Property modelo() As cls_modelo_
        Get
            If IsNothing(obj_modelo) Then
                obj_modelo = New cls_modelo_
                If Not obj_modelo.Buscar(idmodelo) Then
                    obj_modelo = Nothing
                End If
            End If
            modelo = obj_modelo
        End Get
        Set(ByVal value As cls_modelo_)
            obj_modelo = value
        End Set
    End Property


    Property Items() As Collections.Generic.List(Of cls_marcamodelo_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_marcamodelo_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_marcamodelo_)
        While obj_Reader.Read()
            Dim Elemento As New cls_marcamodelo_
            Elemento.int_idmarcamodelo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmarcamodelo")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmarca"))) = False Then
                Elemento.int_idmarca = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmarca")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodelo"))) = False Then
                Elemento.int_idmodelo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodelo")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idmarcamodelo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmarcamodelo")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmarca"))) = False Then
            int_idmarca = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmarca")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodelo"))) = False Then
            int_idmodelo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodelo")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "marcamodelo__Nuevo"
                .Parameters.Add("@idmarca", SqlDbType.Int).Value = int_idmarca
                .Parameters.Add("@idmodelo", SqlDbType.Int).Value = int_idmodelo
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de marcamodelo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "marcamodelo__Modificar"
                .Parameters.Add("@idmarcamodelo", SqlDbType.Int).Value = int_idmarcamodelo
                .Parameters.Add("@idmarca", SqlDbType.Int).Value = int_idmarca
                .Parameters.Add("@idmodelo", SqlDbType.Int).Value = int_idmodelo
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de marcamodelo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idmarcamodelo As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "marcamodelo__Eliminar"
                .Parameters.Add("@idmarcamodelo", SqlDbType.Int).Value = idmarcamodelo
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de marcamodelo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idmarcamodelo As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "marcamodelo__Buscar"
                .Parameters.Add("@idmarcamodelo", SqlDbType.Int).Value = idmarcamodelo
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de marcamodelo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "marcamodelo__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de marcamodelo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "marcamodelo__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de marcamodelo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function
    Public Function CargarXidmarca(ByRef idmarca As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidmarca = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "marcamodelo__CargarXidmarca"
                .Parameters.Add("@idmarca", SqlDbType.Int).Value = idmarca
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXidmarca = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarXidmarca los registros de marcamodelo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function
    Public Function CargarXidmodelo(ByRef idmodelo As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidmodelo = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "marcamodelo__CargarXidmodelo"
                .Parameters.Add("@idmodelo", SqlDbType.Int).Value = idmodelo
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXidmodelo = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarXidmarca los registros de marcamodelo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function
    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_marcamodelo_Clon As New cls_marcamodelo_
        obj_marcamodelo_Clon.int_idmarcamodelo = Me.int_idmarcamodelo
        obj_marcamodelo_Clon.int_idmarca = Me.int_idmarca
        obj_marcamodelo_Clon.int_idmodelo = Me.int_idmodelo
        obj_marcamodelo_Clon.col_Items = Me.col_Items
        Return obj_marcamodelo_Clon
    End Function

End Class
